/*package com.calciominoBeta;


import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import org.json.JSONException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

import com.calciominoBeta.Activity.AQDetailActivity;
import com.calciominoBeta.Entity.InfoOj;
import com.calciominoBeta.Util.Util;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;
import com.telpoo.frame.utils.Mlog;

public class GCMIntentService extends GCMBaseIntentService {
	static final String SERVER_URL = "http://naq.name.vn/gcm/gcmsv/register.php";
	public static final String SENDER_ID = "332773246133";
	public static final String DISPLAY_MESSAGE_ACTION = "com.calciominoBeta.DISPLAY_MESSAGE";
	public static final String EXTRA_MESSAGE = "message";
	private String key_filter = "price";
	// private static final String TAG = "GCMIntentService";
	String name = "";
	String email = "";

	public GCMIntentService() {
		super(SENDER_ID);
	}

	static void displayMessage(Context context, String message) {
		Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
		intent.putExtra(EXTRA_MESSAGE, message);
		context.sendBroadcast(intent);
	}

	*//**
	 * Method called on device registered
	 **//*
	@Override
	protected void onRegistered(Context context, String registrationId) {
		Mlog.I(TAG + "Device registered: regId = " + registrationId);
		displayMessage(context, getString(R.string.registed_gcm));
		Log.d("NAME", name);
		
		
		
		ServerUtilities.register(context, name, email, registrationId);
	}

	*//**
	 * Method called on device un registred
	 * *//*
	@Override
	protected void onUnregistered(Context context, String registrationId) {
		Mlog.I(TAG + "Device unregistered");

		displayMessage(context, getString(R.string.gcm_unregistered));
		ServerUtilities.unregister(context, registrationId);
	}

	*//**
	 * Method called on Receiving a new message
	 * *//*
	@Override
	protected void onMessage(Context context, Intent intent) {

		
		String message = intent.getExtras().getString(key_filter);
		Log.w("telpoo", "message=" + message);
		displayMessage(context, message);
		// notifies user
		generateNotification(context, message);
	}

	*//**
	 * Method called on receiving a deleted message
	 * *//*
	@Override
	protected void onDeletedMessages(Context context, int total) {
		Mlog.I(TAG + "Received deleted messages notification");
		String message = getString(R.string.gcm_deleted, total);
		displayMessage(context, message);
		// notifies user
		generateNotification(context, message);
	}

	*//**
	 * Method called on Error
	 * *//*
	@Override
	public void onError(Context context, String errorId) {
		Mlog.I(TAG + "Received error: " + errorId);
		displayMessage(context, getString(R.string.gcm_error, errorId));
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId) {
		// log message
		Mlog.I(TAG + "Received recoverable error: " + errorId);
		displayMessage(context, getString(R.string.gcm_recoverable_error, errorId));
		return super.onRecoverableError(context, errorId);
	}

	
	
	*//**
	 * Issues a notification to inform the user that server has sent a message.
	 *//*
	public static void generateNotification(Context context, String message) {
		InfoOj oj=null;
		try {
			BaseObject ojGcm= JsonSupport.jsonObject2BaseOj(message);
			ArrayList<BaseObject> ojs=new ArrayList<BaseObject>();
			ojs.add(ojGcm);
			ArrayList<InfoOj> ojg= Util.convertOj(ojs);
			if(ojg!=null&&ojg.size()>0)
			oj=ojg.get(0);
			
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		//wrong message
		if(oj==null) return;
		
		
		
		int icon = R.drawable.ic_launcher;
		long when = System.currentTimeMillis();
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, ""+oj.get("message"), when);

		String title = context.getString(R.string.app_name);

		Intent notificationIntent = new Intent(context, AQDetailActivity.class);
		notificationIntent.putExtra("gcm_message", "" + message);
		// set intent so it does not start a new activity

		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, title, ""+oj.get("message"), intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		// Play default notification sound
		notification.defaults |= Notification.DEFAULT_SOUND;

		// notification.sound = Uri.parse("android.resource://" +
		// context.getPackageName() + "your_sound_file_name.mp3");

		// Vibrate if vibrate is enabled
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notificationManager.notify(0, notification);

	}

	public final static class ServerUtilities {
		private static final int MAX_ATTEMPTS = 5;
		private static final int BACKOFF_MILLI_SECONDS = 2000;
		private static final Random random = new Random();

		*//**
		 * Register this account/device pair within the server.
		 * 
		 *//*
		public static void register(final Context context, String Area, String status, final String regId) {
			Log.i(TAG, "registering device (regId = " + regId + ")");

			// String
			// res=BaseNetSupport.method_GET(Defi.URL_REGISTER_PUSH+"deviceToken="+regId+"&Area="+Area+"&status="+status,
			// null);

			// Mlog.D("register PUSH, response from vietbao:"+res);
			String serverUrl = SERVER_URL;
			Map<String, String> params = new HashMap<String, String>();
			params.put("regId", regId);

			long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
			// Once GCM returns a registration id, we need to register on our
			// server
			// As the server might be down, we will retry it a couple
			// times.
			for (int i = 1; i <= MAX_ATTEMPTS; i++) {
				Log.d(TAG, "Attempt #" + i + " to register");
				try {
					displayMessage(context, context.getString(R.string.server_registering, i, MAX_ATTEMPTS));
					// post(serverUrl, params);
					GCMRegistrar.setRegisteredOnServer(context, true);
					String message = context.getString(R.string.server_registered);
					displayMessage(context, message);
					return;
				} catch (Exception e) {
					// Here we are simplifying and retrying on any error; in a
					// real
					// application, it should retry only on unrecoverable errors
					// (like HTTP error code 503).
					Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
					if (i == MAX_ATTEMPTS) {
						break;
					}
					try {
						Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
						Thread.sleep(backoff);
					} catch (InterruptedException e1) {
						// Activity finished before we complete - exit.
						Log.d(TAG, "Thread interrupted: abort remaining retries!");
						Thread.currentThread().interrupt();
						return;
					}
					// increase backoff exponentially
					backoff *= 2;
				}
			}
			String message = context.getString(R.string.server_register_error, MAX_ATTEMPTS);
			displayMessage(context, message);
		}

		*//**
		 * Unregister this account/device pair within the server.
		 *//*
		static void unregister(final Context context, final String regId) {
			Log.i(TAG, "unregistering device (regId = " + regId + ")");
			String serverUrl = SERVER_URL + "/unregister";
			Map<String, String> params = new HashMap<String, String>();
			params.put("regId", regId);
			try {
				post(serverUrl, params);
				GCMRegistrar.setRegisteredOnServer(context, false);
				String message = context.getString(R.string.server_unregistered);
				displayMessage(context, message);
			} catch (IOException e) {
				// At this point the device is unregistered from GCM, but still
				// registered in the server.
				// We could try to unregister again, but it is not necessary:
				// if the server tries to send a message to the device, it will
				// get
				// a "NotRegistered" error message and should unregister the
				// device.
				String message = context.getString(R.string.server_unregister_error, e.getMessage());
				displayMessage(context, message);
			}
		}

		*//**
		 * Issue a POST request to the server.
		 * 
		 * @param endpoint
		 *            POST address.
		 * @param params
		 *            request parameters.
		 * 
		 * @throws IOException
		 *             propagated from POST.
		 *//*
		private static void post(String endpoint, Map<String, String> params) throws IOException {

			URL url;
			try {
				url = new URL(endpoint);
			} catch (MalformedURLException e) {
				throw new IllegalArgumentException("invalid url: " + endpoint);
			}
			StringBuilder bodyBuilder = new StringBuilder();
			Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
			// constructs the POST body using the parameters
			while (iterator.hasNext()) {
				Entry<String, String> param = iterator.next();
				bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
				if (iterator.hasNext()) {
					bodyBuilder.append('&');
				}
			}
			String body = bodyBuilder.toString();
			Log.v(TAG, "Posting '" + body + "' to " + url);
			byte[] bytes = body.getBytes();
			HttpURLConnection conn = null;
			try {
				Log.e("URL", "> " + url);
				conn = (HttpURLConnection) url.openConnection();
				conn.setDoOutput(true);
				conn.setUseCaches(false);
				conn.setFixedLengthStreamingMode(bytes.length);
				conn.setRequestMethod("POST");
				conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				// post the request
				OutputStream out = conn.getOutputStream();
				out.write(bytes);
				out.close();
				// handle the response
				int status = conn.getResponseCode();
				if (status != 200) {
					throw new IOException("Post failed with error code " + status);
				}
			} finally {
				if (conn != null) {
					conn.disconnect();
				}
			}
		}
	}

	public abstract static class WakeLocker {
		private static PowerManager.WakeLock wakeLock;

		public static void acquire(Context context) {
			if (wakeLock != null)
				wakeLock.release();

			PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
			wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "WakeLock");
			wakeLock.acquire();
		}

		public static void release() {
			if (wakeLock != null)
				wakeLock.release();
			wakeLock = null;
		}
	}

}
*/